<template>
  <div>
    <div>
      <el-form :inline="true" :model="documentVideo" class="demo-form-inline">

        <el-form-item label="文档视频类型编号">
          <el-input v-model="documentVideo.id" placeholder="请输入文档视频类型编号"></el-input>
        </el-form-item>
        <el-form-item label="文档视频名">
          <el-input v-model="documentVideo.name" placeholder="请输入文档视频名"></el-input>
        </el-form-item>
        <el-form-item label="文档视频类型名">
          <el-select v-model="documentVideo.type" placeholder="请选择" >
            <el-option
                v-for="item in documentVideoTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="文档视频状态">
          <el-select v-model="documentVideo.status" placeholder="请选择" >
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="未发布" value="未发布"></el-option>
            <el-option label="未审核" value="未审核"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="show">查询</el-button>
          <el-button type="primary" @click="clear">清空</el-button>
        </el-form-item>

        <el-form-item style="float: left">
          <el-button type="primary" @click="add">新增</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="danger" @click="deleteByIds">批量删除选中</el-button>
        </el-form-item>

      </el-form>

      <div>
        <el-table
            max-height="400"
            highlight-current-row
            ref="multipleTable"
            tooltip-effect="dark"
            border
            @selection-change="handleSelectionChange"
            :data="tableData"
            style="margin: 0 auto; width: 100%;"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="name" label="文档视频名" header-align="center" align="center"></el-table-column>
          <el-table-column prop="type" label="文档视频类型名" header-align="center" align="center">
            <template slot-scope="scope">
              {{getTypeName(scope.row.type)}}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" header-align="center" align="center"></el-table-column>
          <el-table-column label="操作" width="400" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button
                  v-show="scope.row.agencyId===0&&myInfo.role==='机构管理员'"
                  size="mini"
                  type="primary"
                  plain
                  @click=""
              >共享视频文档无权操作
              </el-button>

              <el-button
                  v-show="scope.row.agencyId!==0||(scope.row.agencyId===0&&myInfo.role==='超级管理员')"
                  size="mini"
                  type="primary"
                  plain
                  @click="updateInfo(scope.row.id)"
              >编辑
              </el-button>

              <el-button
                  v-show="scope.row.agencyId!==0||(scope.row.agencyId===0&&myInfo.role==='超级管理员')"
                  size="mini"
                  type="danger"
                  plain
                  @click="deleteById(scope.row.id)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog title="新增" :visible.sync="dialogVisible" :before-close="close">
        <el-form size="mini">
          <el-form-item label="文件类型">
              <el-select v-model="type" placeholder="请选择" style="width:100%">
                <el-option
                    v-for="item in documentVideoTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.id"
                />
              </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-upload
                class="upload-demo"
                drag
                action="http://localhost:8080/upload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-success="fileUpload"
                multiple
                :file-list="fileList">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="close()">取 消</el-button>
          <el-button type="primary" @click="addForm()">确 定</el-button>
        </div>
      </el-dialog>



      <el-dialog title="编辑" :visible.sync="updateVisible" :before-close="clearForm">
        <el-form v-model="updateData" size="mini">

          <el-form-item label="文件类型">
            <el-select v-model="updateData.type" placeholder="请选择" style="width:100%">
              <el-option
                  v-for="item in documentVideoTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <a target='_blank' v-if="!getDocumentOrViewType()" :href='getUrl()'>预览文档文件</a>
            <video  width="100%" controls v-if="getDocumentOrViewType()">
              <source :src="getUrl()" type="video/mp4">
              您的浏览器不支持视频标签。
            </video>
          </el-form-item>
          <el-form-item label="文件名">
            <el-input v-model="updateData.name"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="clearForm()">取 消</el-button>
          <el-button type="primary" @click="updateForm()">确 定</el-button>
        </div>
      </el-dialog>

    </div>
    <br>
    <!--分页工具条-->
    <el-pagination
        style="position:absolute;bottom: 20px;margin-left: 40vh"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :background="background"
        :current-page="currentPage"
        :page-sizes="[5, 10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
    </el-pagination>
  </div>


</template>
<script>


import {
  addDocumentVideo,
  changeDocumentVideo,
  deleteDocumentVideo,
  getAllDocumentVideo
} from "@/api/documentVideo";
import {getDocumentVideoTypeList} from "@/api/documentVideoType";
import {getInfo} from "@/api/user";

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 表格数据
      tableData: [],
      documentVideo: {},
      form: {},
      posts: [],
      multipleSelection: [],
      selectedIds: [],
      documentVideoTypeList: [],
      formLabelWidth: "140px",


      //文件上传
      fileList: [],
      addOrUpdateFileList: [],//所有文件上传后的地址
      attachmentInfoShow: false,
      type:'',
      //更新
      updateVisible:false,
      updateData:{},


      myInfo:{},//操作人信息
    }
  },
  methods: {
    show() {
      getAllDocumentVideo(this.documentVideo, this.currentPage, this.pageSize).then((res) => {
        if (res.data.code === 1) {
          this.totalCount = res.data.data.total;
          this.tableData = res.data.data.rows;
          //console.log(this.tableData)
          if (this.tableData.length === 0 && this.currentPage > 1) {
            this.currentPage -= 1;
            this.show();
          }
        }
      }).catch((e) => {
      })
    },
    getDocumentVideoTypeLists() {
      getDocumentVideoTypeList().then((res) => {
        if (res.data.code === 1) {
            this.documentVideoTypeList=res.data.data;
        }else {
          this.$message.error(res.data.msg);
        }
      }).catch((e) => {
      })
    },
    getTypeName(type){
      const types = this.documentVideoTypeList.find(t => t.id === type);
      return types ? types.name : '未找到对应的类型';
    },
    getDocumentOrViewType(){
     console.log( this.getTypeName(this.updateData.type))
      return this.getTypeName(this.updateData.type).endsWith("视频");
    },
    getUrl(){
      return this.updateData.url;
    },
    clear() {
      this.documentVideo = {};
      this.show();
    },
    add() {
      this.documentVideo = {};
      this.addOrUpdateFileList = [];
      this.type=''
      this.dialogVisible = true;
    },
    addForm() {

        for (let i = 0; i < this.addOrUpdateFileList.length; i++) {
          this.addOrUpdateFileList[i].type=this.type;

        addDocumentVideo(this.addOrUpdateFileList).then(res => {
          if (res.data.code === 1) {
            this.$message.success("保存成功")
            this.dialogVisible = false;
            this.addOrUpdateFileList = [];
            this.fileList=[];
            this.show();
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
        });
      }
    },
    updateForm(){
      changeDocumentVideo(this.updateData).then(res => {
        //console.log(res)
        if (res.data.code === 1) {
          this.$message.success("更新成功")
          this.updateVisible = false;
          this.show()
        }else {
          this.$message.error(res.data.msg)
          this.updateVisible = false;
        }
      }).catch(e => {
      });
    },
    clearForm(){
      this.updateData={};
      this.updateVisible=false;
    },

    close() {
      this.dialogVisible = false;
      this.type = "";
      this.addOrUpdateFileList=[];
      this.fileList=[];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    updateInfo(id) {
      this.updateData = this.tableData.filter(data => data.id === id)[0];
      this.updateVisible = true;
    },
    deleteById(id) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(this.tableData.filter(data => data.id === id)[0]);
      this.deleteByIds()
    },
    deleteByIds() {
      // 弹出确认提示框
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //用户点击确认按钮
        //1. 创建id数组, 从 this.multipleSelection 获取即可
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.selectedIds[i] = this.multipleSelection[i].id;
        }
        //console.log(this.selectedIds);
        deleteDocumentVideo(this.selectedIds).then((resp) => {
          if (resp.data.code === 1) {
            //删除成功
            this.$message.success("恭喜你，删除成功");
            this.clear();
          } else {
            this.$message.error(resp.data.msg);
            this.show();
          }
        }).catch(() => {
        });

      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("已取消删除");
      });

    },

    //分页
    handleSizeChange(val) {
      // 重新设置每页显示的条数
      this.pageSize = val;
      this.show();
    },
    handleCurrentChange(val) {
      // 重新设置当前页码
      this.currentPage = val;
      this.show();
    },


    //文件上传
    //删除
    handleRemove(file, fileList) {
      this.addOrUpdateFileList = [];
      for (let i = 0; i < fileList.length; i++) {
        this.addOrUpdateFileList.push(fileList[i].response.data);
      }
    },
    handlePreview(file) {
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    //上传成功
    fileUpload(response, file, fileList) {
      //console.log(response)
      if (response.code === 1) {
        this.addOrUpdateFileList.push(response.data);
      }
    },


    getMyInfo() {
      getInfo().then((res) => {
        if (res.data.code === 1) {
          this.myInfo = res.data.data;
          this.show();
        }
      }).catch((e) => {
      })
    },


  },
  mounted() {
    this.show();
    this.getDocumentVideoTypeLists();
    this.getMyInfo();
  },
};
</script>


<style>
* {
  margin: 0;
  padding: 0;
}

.el-header {
  background-color: rgb(238, 241, 246);
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: white;
}

</style>
<script setup>
</script>
